#include "StdAfx.h"
#include "box_2.h"
#include "segment_2.h"
#include <algorithm>
#include <boost/geometry/algorithms/intersects.hpp> 

bool box_2::within(const point_2& point) const
{
  return (point.x>top_left.x && point.x<bottom_right.x) && \
    (point.y>top_left.y && point.y<bottom_right.y);
}

bool box_2::adjacent(const point_2& point, int tolerance) const
{
  segment_2_ref top_segment(top_left, top_right());
  segment_2_ref bottom_segment(bottom_left(), bottom_right);
  segment_2_ref left_segment(top_left, bottom_left());
  segment_2_ref right_segment(top_right(), bottom_right);

  return top_segment.adjacent(point, tolerance) ||
    bottom_segment.adjacent(point, tolerance) ||
    left_segment.adjacent(point, tolerance) ||
    right_segment.adjacent(point, tolerance);
}

bool box_2::intersects(const box_2& box) const
{
  return boost::geometry::intersects(*this, box);
}

void box_2::normalize(void)
{
  double nTemp = 0;
  if (top_left.x > bottom_right.x)
  {
    nTemp           = top_left.x;
    top_left.x      = bottom_right.x;
    bottom_right.x  = nTemp;
  }

  if (top_left.y  > bottom_right.y)
  {
    nTemp           = top_left.y ;
    top_left.y      = bottom_right.y;
    bottom_right.y  = nTemp;
  }
}

